package net.actions.sys;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.Iterator;

import net.base.BaseAction;
import net.base.BaseDao;

import org.apache.log4j.Logger;

public class RoleAction extends BaseAction {
    private BaseDao dao = null;

    public BaseDao getDao() {
        return dao;
    }

    public void setDao(BaseDao dao) {
        this.dao = dao;
    }

    private Logger log = Logger.getLogger(this.getClass());

    public void deleteRole() throws Exception {
        try {
            Map map = this.parserRequest(this.getRequest());
            String[] role_dm = String.valueOf(map.get("IDS")).split(",");

            // 循环删除角色
            for (int n = 0; n < role_dm.length; n++) {
                // 先删除角色模块的关系
                dao.delete("role.deleteRoleModuleByRoleDM", role_dm[n]);
                //删除角色用户的关系
                dao.delete("role.deleteRoleUserByRoleDM", role_dm[n]);
                // 删除角色
                dao.delete("role.deleteRole", role_dm[n]);

            }
            this.setResult("00", "删除成功！");
        } catch (Exception e) {
            log.error(e.toString());
            this.setResult("99", "删除失败！\n" + e.toString());
            throw new Exception();
        }
    }

    /**
     * 根据角色代码取角色的权限树
     */
    public void getModuleTreeByRoleDM() {
        try {
            StringBuffer xml = new StringBuffer();
            String module_dm_sj = this.getRequest().getParameter("MODULE_DM_SJ");
            String role_dm = this.getRequest().getParameter("ROLE_DM");
            String user_dm = this.getRequest().getParameter("USER_DM");
            xml.append("<?xml version=\"1.0\" encoding=\"GBK\"?>");
            xml.append("<tree id=\"0\">");
            xml.append(getTreeString(module_dm_sj, role_dm,user_dm));
            xml.append("</tree>");
            this.setResult("00", xml.toString());
        } catch (Exception e) {
            log.error(e.toString());
            this.setResult("99", "操作失败！\n" + e.toString());
        }
    }

    /**
     * 取角色的权限树xml
     *
     * @param module_dm_sj
     * @param role_dm
     * @return
     * @throws Exception
     */
    private String getTreeString(String module_dm_sj, String role_dm,String user_dm) throws Exception {
        StringBuffer xml = new StringBuffer();
        Map parMap = new HashMap();
        parMap.put("MODULE_DM_SJ", module_dm_sj);
        parMap.put("ROLE_DM", role_dm);
        parMap.put("USER_DM", user_dm);
        List list = dao.getList("role.selectModuleByRoleDM", parMap);
        Iterator it = list.iterator();
        while (it.hasNext()) {
            Map m = (Map) it.next();
            String check = "";
            if ("1".equals(m.get("LX"))) {  //叶子节点才添加选中操作
                check = (String) m.get("BZ");
            }
            xml.append("<item text=\"" + m.get("MODULE_NAME")).append("\"").
                    append(" id=\"").append(m.get("MODULE_DM")).append("\" checked=\"").append(check)
                    .append("\" im0=\"tombs.gif\" im1=\"folderOpen.gif\" im2=\"iconSafe.gif\">");
            xml.append(getTreeString(String.valueOf(m.get("MODULE_DM")), role_dm,user_dm));
            xml.append("</item>");
        }
        return xml.toString();
    }

    /**
     * 保存角色模块关系
     */
    public void insertRoleModule() throws Exception {
        try {
            String[] module_dms = this.getRequest().getParameter("MODULE_DMS").split(",");
            String role_dm = this.getRequest().getParameter("ROLE_DM");
            dao.delete("role.deleteRoleModuleByRoleDM", role_dm);
            for (int i = 0; i < module_dms.length; i++) {
                Map parMap = new HashMap();
                parMap.put("ROLE_DM", role_dm);
                parMap.put("MODULE_DM", module_dms[i]);
                dao.insert("role.insertRoleModule", parMap);
            }
            this.setResult("00", "模块分配成功！");
        } catch (Exception e) {
            log.error(e.toString());
            this.setResult("99", "模块分配失败！\n" + e.toString());
            throw new Exception();
        }
    }
}
